﻿
// ———————————————————————–
// <copyright file="IncidentDetailGenerator.cs" company="The MITRE Corporation">
//    Copyright (c) 2014 The MITRE Corporation. All rights reserved.
//    Licensed under the Apache License, Version 2.0 (the "License");
//    you may not use this file except in compliance with the License.
//    You may obtain a copy of the License at
//    http://www.apache.org/licenses/LICENSE-2.0
//    Unless required by applicable law or agreed to in writing, software
//    distributed under the License is distributed on an "AS IS" BASIS,
//    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//    See the License for the specific language governing permissions and
//    limitations under the License.
// </copyright>
// ———————————————————————–
/////////////////////////////////////////////////////////////////////////////////////////////////
// IncidentDetailGenerator.cs:
//
// A generator for good and bad examples of IncidentDetail.
//
// Project: NIEMCADTest
// Author:  Lizzie DeYoung - MITRE
/////////////////////////////////////////////////////////////////////////////////////////////////

// Copyright (c) 2014 The MITRE Corporation. All rights reserved.
//
// NOTICE
// This software was produced for the U. S. Government
// under Contract No. FA8721-09-C-0001, and is
// subject to the Rights in Noncommercial Computer Software
// and Noncommercial Computer Software Documentation Clause
// (DFARS) 252.227-7014 (JUN 1995)  
      

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace NIEMCADTest.IncidentDetailTests
{
  class IncidentDetailGenerator : Generator
  {
    public const string STATUS_SUB = "$status$";
    public const string OWNING_ORG_SUB = "$owningOrg$";
    public const string LOCATION_SUB = "$location$";

    public const string INCIDENT_DETAIL = @"
    <$prefix$:$name$ xmlns:$prefix$='$namespace$'>
      $status$
      $owningOrg$
      $location$
    </$prefix$:$name$>";

    public const string WRONG_ELEMENT_NAME = @"
    <$prefix$:asdf xmlns:$prefix$='$namespace$'>
      $status$
      $owningOrg$
      $location$
    </$prefix$:asdf>";

    public IncidentDetailGenerator()
    {
      elementNamespace = "http://mitre.org/emevent/0.8/cad2cad/";
      elementName = "IncidentDetail";
      elementPrefix = "cad";
    }


    public List<TestObject> BAD_WrongElementName()
    {
      List<TestObject> list = new List<TestObject>();

      // grab good content
      TestObject statusTestObject = TestHelper.GetAGoodTestObject(typeof(IncidentStatusGenerator));
      string statusXmlText = statusTestObject.Xml.OuterXml;
      TestObject owningOrgTestObject = TestHelper.GetAGoodTestObject(typeof(IncidentOwningOrganizationGenerator));
      string owningOrgXmlText = owningOrgTestObject.Xml.OuterXml;

      // add content
      string xml = GetXml(WRONG_ELEMENT_NAME);
      xml = xml.Replace(STATUS_SUB, statusXmlText);
      xml = xml.Replace(OWNING_ORG_SUB, owningOrgXmlText);

      TestObject testObject = GetTestObject(xml);

      list.Add(testObject);

      return list;
    }

    public List<TestObject> BAD_MultipleElements()
    {
      List<TestObject> list = new List<TestObject>();
      TestObject testObject;
      string xml;

      // grab good content
      TestObject statusTestObject = TestHelper.GetAGoodTestObject(typeof(IncidentStatusGenerator));
      string statusXmlText = statusTestObject.Xml.OuterXml;
      TestObject owningOrgTestObject = TestHelper.GetAGoodTestObject(typeof(IncidentOwningOrganizationGenerator));
      string owningOrgXmlText = owningOrgTestObject.Xml.OuterXml;

      // add content twice for status
      xml = GetXml(INCIDENT_DETAIL);
      xml = xml.Replace(STATUS_SUB, statusXmlText + statusXmlText);
      xml = xml.Replace(OWNING_ORG_SUB, owningOrgXmlText);
      testObject = GetTestObject(xml);
      list.Add(testObject);

      // add content twice for owning org
      xml = GetXml(INCIDENT_DETAIL);
      xml = xml.Replace(STATUS_SUB, statusXmlText);
      xml = xml.Replace(OWNING_ORG_SUB, owningOrgXmlText + owningOrgXmlText);
      testObject = GetTestObject(xml);
      list.Add(testObject);

      return list;
    }

    public List<TestObject> BAD_MissingElements()
    {
      List<TestObject> list = new List<TestObject>();
      TestObject testObject;
      string xml;

      // grab good content
      TestObject statusTestObject = TestHelper.GetAGoodTestObject(typeof(IncidentStatusGenerator));
      string statusXmlText = statusTestObject.Xml.OuterXml;
      TestObject owningOrgTestObject = TestHelper.GetAGoodTestObject(typeof(IncidentOwningOrganizationGenerator));
      string owningOrgXmlText = owningOrgTestObject.Xml.OuterXml;

      // completely empty
      xml = GetXml(INCIDENT_DETAIL);
      xml = xml.Replace(STATUS_SUB, "");
      xml = xml.Replace(OWNING_ORG_SUB, "");
      testObject = GetTestObject(xml);
      list.Add(testObject);

      // empty status
      xml = GetXml(INCIDENT_DETAIL);
      xml = xml.Replace(STATUS_SUB, "");
      xml = xml.Replace(OWNING_ORG_SUB, owningOrgXmlText);
      testObject = GetTestObject(xml);
      list.Add(testObject);

      // empty org
      xml = GetXml(INCIDENT_DETAIL);
      xml = xml.Replace(STATUS_SUB, statusXmlText);
      xml = xml.Replace(OWNING_ORG_SUB, "");
      testObject = GetTestObject(xml);
      list.Add(testObject);;

      return list;
    }

    public List<TestObject> BAD_BadContent()
    {
      // grab good content
      TestObject statusTestObject = TestHelper.GetAGoodTestObject(typeof(IncidentStatusGenerator));
      string statusXmlText = statusTestObject.Xml.OuterXml;
      TestObject owningOrgTestObject = TestHelper.GetAGoodTestObject(typeof(IncidentOwningOrganizationGenerator));
      string owningOrgXmlText = owningOrgTestObject.Xml.OuterXml;

      // grab bad lists
      List<TestObject> badStatusList = TestHelper.GetBadTestObjects(typeof(IncidentStatusGenerator));
      List<TestObject> badOwningOrgList = TestHelper.GetBadTestObjects(typeof(IncidentOwningOrganizationGenerator));

      string starterXml = GetXml(INCIDENT_DETAIL);

      // put it all together
      List<TestObject> list = new List<TestObject>();
      foreach(TestObject testObject in badStatusList) {
        string xml = starterXml;
        xml = xml.Replace(STATUS_SUB, testObject.Xml.OuterXml);
        xml = xml.Replace(OWNING_ORG_SUB, owningOrgXmlText);

        TestObject newTestObject = GetTestObject(xml);
        list.Add(newTestObject);
      }
      foreach (TestObject testObject in badOwningOrgList)
      {
        string xml = starterXml;
        xml = xml.Replace(STATUS_SUB, statusXmlText);
        xml = xml.Replace(OWNING_ORG_SUB, testObject.Xml.OuterXml);

        TestObject newTestObject = GetTestObject(xml);
        list.Add(newTestObject);
      }
      
      return list;
    }

    public List<TestObject> GOOD_GoodContent()
    {
      // grab good lists
      List<TestObject> goodStatusList = TestHelper.GetGoodTestObjects(typeof(IncidentStatusGenerator));
      List<TestObject> goodOwningOrgList = TestHelper.GetGoodTestObjects(typeof(IncidentOwningOrganizationGenerator));

      List<TestObject> list = new List<TestObject>();
      string starterXml = GetXml(INCIDENT_DETAIL);

      //without controlling org
      foreach (TestObject statusTestObject in goodStatusList)
      {
        foreach (TestObject owningOrgTestObject in goodOwningOrgList)
        {
          string xml = starterXml;
          xml = xml.Replace(STATUS_SUB, statusTestObject.Xml.OuterXml);
          xml = xml.Replace(OWNING_ORG_SUB, owningOrgTestObject.Xml.OuterXml);
          TestObject testObject = GetTestObject(xml);
          list.Add(testObject);
        }
      }
      return list;
    }

    //TODO: TEST do location testing once it gets implemented.
    protected override string GetXml(string xml)
    {
      string newXml = base.GetXml(xml);
      newXml = newXml.Replace(LOCATION_SUB, "");

      return newXml;
    }
  }
}
